package com.xcc.observerpattern.demo2;

import com.xcc.observerpattern.demo1.DisplayElement;
import lombok.Data;

import java.util.Observable;
import java.util.Observer;

/**
 * /**
 *
 * @Description WeatherData
 * @Author xiechuang
 * @Date 2022/7/21
 */
@Data
public class CurrentConditionsDisplay implements Observer, DisplayElement {

    Observable observable;
    // 温度值
    private float temperature;
    // 湿度值
    private float humidity;

    public CurrentConditionsDisplay(Observable observable) {
        this.observable = observable;
        observable.addObserver(this);
    }

    public void update(Observable o, Object arg) {
        if (o instanceof WeatherData) {
            WeatherData weatherData = (WeatherData) o;
            this.temperature = weatherData.getTemperature();
            this.humidity = weatherData.getHumidity();
            display();
        }
    }

    public void display() {
        System.out.println("CurrentConditionsDisplay 当前温度：" + temperature + "，湿度：" + humidity);
    }

}
